#!/usr/bin/python
# -*- coding: utf-8 -*-

import fileinput, sys, os

def get_name(s):
  s = s.strip()
  p = s.rfind("/")
  if p != -1: s = s[p+1:]
  if s.endswith(".jpg"): s = s[:-4]
  if s.endswith("_s"): return []
  return s.split("_")

def gen_card(args):
  vers = args[0]
  s_element = args[1][0]
  s_ctype = args[2][0]
  name = args[3:]

  elements = {"a":"air", "d":"darkness", "e":"earth", "f":"fire", "l":"light", "n":"neutral", "w":"water"}
  ctypes = {"a":"ability", "c":"creature", "l":"land", "s":"spell"}
  ctypes_p = {"a":"abilities", "c":"creatures", "l":"lands", "s":"spells"}

  element = elements[s_element]
  ctype = ctypes[s_ctype]
  ctype_p = ctypes_p[s_ctype]

  id = "%s_%s_%s_%s" % (vers, s_element, s_ctype, "_".join(name))
  namespace = "Ragima.Cards%s.%s.%s" % (vers, element.capitalize(), ctype_p.capitalize())
  classname = "".join(map(lambda s: s.capitalize(), name))
  en_name = " ".join(name)

  print "Generate card %s.%s" % (namespace, classname)

  fname = element.capitalize() + os.sep + classname + ".cs"
  #f = sys.stdout
  f = open(fname, "w")
  fh = open("template_cs.txt", "r")
  for s in fh.readlines(): f.write(s)
  fh.close()

  f.write("namespace "+namespace+" {\n")
  f.write("    public class %s: %sCard {\n" % (classname, ctype.capitalize()))
  f.write("        public %s(): base(\"%s\") {\n" % (classname, id))
  f.write("            Version = 0;\n")
  f.write("            Element = ElementType.%s;\n" % (element.capitalize()))
  if ctype != "ability":
    f.write("            Value = 100;\n")
  if ctype == "creature":
    f.write("            Damage = 0;\n")
    f.write("            Health = 0;\n")
  f.write("        }\n")
  f.write("/* вставить в языковые файлы:\n")
  f.write("                new ResourceGroup(\"%s.\", new ResourceItem[] {\n" % (id))
  f.write("                    RussianName.New(\"name\", \"\"),\n")
  f.write("                    RussianPhrase.New(\"desc\", \"\"),\n")
  f.write("                }),\n")
  f.write("                new ResourceGroup(\"%s.\", new ResourceItem[] {\n" % (id))
  f.write("                    EnglishName.New(\"name\", \"%s\"),\n" % (en_name))
  f.write("                    EnglishPhrase.New(\"desc\", \"\"),\n")
  f.write("                }),\n")
  f.write("*/\n")
  f.write("    }\n")
  f.write("}\n")

  f.close()

  os.system("svn add %s" % (fname))
  os.system("svn ps svn:keywords Id %s" % (fname))

print "GenCards script v 1.1"
if len(sys.argv) < 2:
  print "Usage: gen_cards.py <file> [file] ..."
  print "       gen_cards.py -s <vers> <element> <type> <id>"
  exit()

if sys.argv[1] == "-s":
  gen_card(sys.argv[2:])
else:
  for line in fileinput.input():
    name = get_name(line)
    if name and len(name) > 3: gen_card(name)
